package com.yanqu.road.dao.impl.activity.langyamemorial;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.langyamemorial.CrossLangYaMemorialUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossLangYaMemorialUserDataDaoImpl extends TempDataObjectDao<CrossLangYaMemorialUserData> {


    public static String getCrossLangYaMemorialUserDataSql = "select * from `t_c_activity_langya_memorial_user_data` where `activityId`=? ";
    public Map<Long, CrossLangYaMemorialUserData> getCrossLangYaMemorialUserData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, CrossLangYaMemorialUserData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossLangYaMemorialUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossLangYaMemorialUserData data = parse(rs);
                result.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String updateCrossLangYaMemorialUserDataSql = "update `t_c_activity_langya_memorial_user_data` set buildNo=?,updateTime=?,buildValue=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(CrossLangYaMemorialUserData crossLangYaMemorialUserData) {
        crossLangYaMemorialUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialUserData.getBuildNo()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialUserData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialUserData.getBuildValue()));
        para.put(i++, new DbParameter(Types.INTEGER, crossLangYaMemorialUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialUserData.getUserId()));
        if (getDb().executeNoneQuery(updateCrossLangYaMemorialUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertCrossLangYaMemorialUserDataSql = "insert into `t_c_activity_langya_memorial_user_data`(activityId,userId,buildNo,updateTime,buildValue) values (?,?,?,?,?)";
    @Override
    protected boolean addData(CrossLangYaMemorialUserData crossLangYaMemorialUserData) {
        crossLangYaMemorialUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, crossLangYaMemorialUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialUserData.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialUserData.getBuildNo()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialUserData.getUpdateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, crossLangYaMemorialUserData.getBuildValue()));
        if (getDb().executeNoneQuery(insertCrossLangYaMemorialUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected CrossLangYaMemorialUserData parseData(ResultSet rs) throws SQLException {
        CrossLangYaMemorialUserData data = new CrossLangYaMemorialUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setBuildNo(rs.getLong("buildNo"));
        data.setUpdateTime(rs.getLong("updateTime"));
        data.setBuildValue(rs.getLong("buildValue"));

        return data;
    }
}
